package org.eproject.core.well.factory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class Readconfig {
	private static Map<String,String> configXml;
	private static Map<String,String> analyse(String path){
		Map<String,String> tempxml = new HashMap<String,String>();
		SAXBuilder sb = new SAXBuilder();
		Document doc;
		try {
			doc = sb.build(path);
		Element root = doc.getRootElement();
		List<?> l1 = root.getChildren();
		for(Iterator<?> it1 = (Iterator<?>)l1.iterator();it1.hasNext();){
			Element chil = (Element)it1.next();
			List<?> l2 = chil.getChildren();
			for(Iterator<?> it2 = (Iterator<?>)l2.iterator();it2.hasNext();){
				Element temp = (Element)it2.next();
				tempxml.put(temp.getName(),temp.getTextTrim());
				System.out.println(temp.getName()+" "+temp.getText());
			}
		}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return tempxml;
	}
	
	private static String getPath(){
		String path = Readconfig.class.getClassLoader().getResource("").getPath()+"Config.xml";
		return path;
	}
	
	public static Map<String,String> getXmlConfig(){
		if(configXml != null)
			return configXml;
		configXml = analyse(getPath());
		return configXml;
	}
	
}
